package com.ddz.shortcut

import android.accessibilityservice.AccessibilityService
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.Toast

class DemoAccessibilityService : AccessibilityService(), JKShortcutListener {

    override fun onServiceConnected() {
        super.onServiceConnected()
        JKShortcut.init(this)
        val arrayJKDisposedModule = arrayListOf<JKDisposedModule>()
        arrayJKDisposedModule.add(
            JKDisposedModule(
                "跳过广告",
                null,
                JKShortcut.MATCH_TEXT,
                "(?=^.{2,4}\$)(?=.*跳过.*)|(?=.*跳过.*)(?=.*[0-9].*)",
                JKShortcut.EXE_TYPE_CLICK,
                null,
                null
            )
        )
        JKShortcut.loadDisposedModule(arrayJKDisposedModule)
        JKShortcut.setEventIntervalTimeMillis(200)
        JKShortcut.setClickIntervalTimeMillis(1000)
        JKShortcut.setListener(this)

//        thread {
//            while (true) {
//                Thread.sleep(1000)
//                val max = (Runtime.getRuntime().maxMemory().toFloat() / (1024f * 1024f))
//                val total = (Runtime.getRuntime().totalMemory().toFloat() / (1024f * 1024f))
//                JKShortcutLog.v("内存","最大内存:${max},使用内存:$total")
//            }
//        }
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        JKShortcut.event(event)
    }

    override fun onInterrupt() {
    }

    override fun onDisposedState(
        state: Int,
        view: AccessibilityNodeInfo,
        module: JKDisposedModule
    ) {
        if (state == JKShortcut.DISPOSED_STATE_SUCCESS) {
            Toast.makeText(this, "广告跳过成功", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onError(e: Exception) {
        Toast.makeText(this, "出BUG了", Toast.LENGTH_SHORT).show()
    }
}